package cn.maihe.elg.operation.centers.guogu.auth;

import cn.maihe.elg.operation.utils.StringUtil;
import cn.maihe.elg.operation.utils.ValidateUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @Description
 * @Author SSX
 * @Date 2025/03/18
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GuoGuHeaderInfo {
    /**
     * 请求凭证，用于识别访问方身份。
     */
    @NotBlank
    private String authorization;

    /**
     * 请求方法，比如投保信息推送接口的请求方法为insure_apply
     */
    @NotBlank
    private String method;
    /**
     * 签名，用于请求接收方验签。 SM3({data}&{client_id}&{client_secret})
     */
    @NotBlank
    private String signature;

    public static GuoGuHeaderInfo of(Map<String, Object> headerMap) {
        GuoGuHeaderInfo headerInfo = new GuoGuHeaderInfo();
        String authorization = StringUtil.isNotBlank((String)headerMap.get("authorization"))?
                (String)headerMap.get("authorization"):(String)headerMap.get("Authorization");
        headerInfo.setAuthorization(authorization);
        headerInfo.setMethod((String) headerMap.get("method"));
        headerInfo.setSignature((String) headerMap.get("signature"));
        try {
            ValidateUtil.validate(headerInfo);
            return headerInfo;
        } catch (Exception e) {
            throw new RuntimeException("请求头参数错误:" + e.getMessage(), e);
        }
    }
}
